//
//  WMFoodsViewController.m
//  witMorning
//
//  Created by Mac mini on 2018/1/3.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "WMFoodsViewController.h"
#import "WMFoodsHeadView.h"
#import "WMFoodsListCell.h"

@interface WMFoodsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *table;

@end

@implementation WMFoodsViewController
{
    NSMutableArray<FoodModel *> *foodArray;
    FoodModel *model;
    NSArray *daysArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"智慧食堂";
    self.view.backgroundColor = [UIColor whiteColor];
    
   
    
    WMFoodsHeadView *view = [[WMFoodsHeadView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_W, 50)];
   
    daysArray = [WMTool daysOfCurrentMonth];
    view.dateArray = daysArray;
    [self.view addSubview:view];
    
    view.FoodHeadCallBack = ^(NSInteger index) {
        [self refreshModel:index];
    };
 
    [self createTable];
    
    [self requestData];
    
}

-(void)refreshModel:(NSInteger)index{
    NSString *str;
    if ([daysArray[index] integerValue]<10) {
        str = [NSString stringWithFormat:@"-0%ld",[daysArray[index] integerValue]];
    }else{
        str = [NSString stringWithFormat:@"-%ld",[daysArray[index] integerValue]];
    }
    
    NSString *time = [[WMTool currrentWithFormatterStyle:@"yyyy-MM"] stringByAppendingString:str];
    [foodArray enumerateObjectsUsingBlock:^(FoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.menudate isEqualToString:time]) {
            model = obj;
            [_table reloadData];
            *stop = YES;
        }else if (idx==foodArray.count-1) {
            model = nil;
            [_table reloadData];
        }
    }];
}

-(void)requestData{
    //getCanteenTable
    NSString *url = [BASE_HTTP stringByAppendingString:@"getCanteenTable"];
    [[WMManage shareInstance]getRequestWithUrl:url success:^(id responseObj) {
        NSLog(@"%@",responseObj);
        if (!foodArray) {
            foodArray = [NSMutableArray arrayWithCapacity:0];
        }
        
        for (NSDictionary *dic in responseObj[@"result"]) {
            FoodModel *model = [[FoodModel alloc]initWithDic:dic];
            [foodArray addObject:model];
        }
        
        [self refreshModel:0];
        
    } fail:^(id error) {
        NSLog(@"%@",error);
    }];
}


-(void)createTable{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT+50, SCREEN_W, SCREEN_H-NAV_HEIGHT-50) style:UITableViewStyleGrouped];
        _table.backgroundColor = [UIColor whiteColor];
        UINib *nib = [UINib nibWithNibName:@"WMFoodsListCell" bundle:nil];
        [_table registerNib:nib forCellReuseIdentifier:identifier];
        
        _table.delegate =self;
        _table.dataSource =self;
        [self.view addSubview:_table];
        
        if (@available(iOS 11.0, *)) {
            _table.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return .1;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, .1)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return model?1:0;
}


static NSString *identifier = @"cellIdentifier";

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WMFoodsListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.model = model;//foodArray[indexPath.row];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender- (void)encodeWithCoder:(nonnull NSCoder *)aCoder {
 <#code#>
 }
 
 - (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection {
 <#code#>
 }
 
 - (void)preferredContentSizeDidChangeForChildContentContainer:(nonnull id<UIContentContainer>)container {
 <#code#>
 }
 
 - (CGSize)sizeForChildContentContainer:(nonnull id<UIContentContainer>)container withParentContainerSize:(CGSize)parentSize {
 <#code#>
 }
 
 - (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(nonnull id<UIContentContainer>)container {
 <#code#>
 }
 
 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator {
 <#code#>
 }
 
 - (void)willTransitionToTraitCollection:(nonnull UITraitCollection *)newCollection withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator {
 <#code#>
 }
 
 - (void)didUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context withAnimationCoordinator:(nonnull UIFocusAnimationCoordinator *)coordinator {
 <#code#>
 }
 
 - (void)setNeedsFocusUpdate {
 <#code#>
 }
 
 - (BOOL)shouldUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context {
 <#code#>
 }
 
 - (void)updateFocusIfNeeded {
 <#code#>
 }
 
 :(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
